package br.com.pnfp.web.control.actions.cadastros;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.pnfp.web.control.actions.DefaultAction;
import br.com.pnfp.web.control.forms.cadastros.CadastroEmpresaForm;
import br.com.pnfp.web.model.enums.Ativo;
import br.com.pnfp.web.model.vo.Empresa;

public class CadastroEmpresaAction extends DefaultAction {
	
	private static final Logger logger = Logger.getLogger(CadastroEmpresaAction.class); 
	
	public ActionForward iniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		return mapping.findForward("body");
	}
	
	public ActionForward consultar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroEmpresaForm empresaForm = (CadastroEmpresaForm) form;
		consultar(empresaForm, request);
		
		return mapping.findForward("body");
	}
	
	public ActionForward incluir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroEmpresaForm empresaForm = (CadastroEmpresaForm) form;
		Empresa vo = new Empresa();
		vo.setNome(empresaForm.getNome());
		vo.setEmailResponsavel(empresaForm.getEmailResponsavel());
		getEmpresaDAO().incluir(vo);
		
		return null;
	}	
	
	public ActionForward alterarIniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroEmpresaForm empresaForm = (CadastroEmpresaForm) form;
		List<Empresa> lista = getEmpresaDAO().consultar(new Empresa(empresaForm.getIdSelecionado()));
		 
		Empresa vo = lista.get(0);
		printJsonObjectToStream(vo, response);
		
		return null;
	}	
	
	public ActionForward alterar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroEmpresaForm empresaForm = (CadastroEmpresaForm) form;
		Empresa vo = new Empresa();
		vo.setCodigo(empresaForm.getIdSelecionado());
		vo.setNome(empresaForm.getNome());
		vo.setEmailResponsavel(empresaForm.getEmailResponsavel());
		getEmpresaDAO().alterar(vo);
		
		return null;
	}
	
	public ActionForward excluir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroEmpresaForm empresaForm = (CadastroEmpresaForm) form;
		getEmpresaDAO().excluir(empresaForm.getIdSelecionado());
		
		return mapping.findForward("body");
	}

	
	private void consultar(CadastroEmpresaForm empresaForm, HttpServletRequest request) {
		Empresa vo = new Empresa();
		vo.setNome(empresaForm.getNomeConsultar());
		vo.setAtivo(Ativo.setValue(empresaForm.getAtivoConsultar()));
		
		List<Empresa> listaResultado = getEmpresaDAO().consultar(vo);
		request.setAttribute("listaResultado", listaResultado);
	}
}
